<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<div class="panel_container">
    <div class="panel">
        <form id="form_org" method="post" action="${ctx}/system/organization/saveOrg">
            <input type="hidden" name="id"/>
            <div class="panel_header">
                <div><h2>机构管理</h2></div>
            </div>
            <div class="panel_body">
                <div>
                    <div>
                        <div class="info_item"><span>14位银行代码:<b>*</b></span>
                            <input type="text" name="orgCode" id="orgCode" maxlength="14"/>
                        </div>
                        <div class="info_item"><span>金融机构名称:<b>*</b></span>
                            <input type="text" name="orgName" id="orgName" maxlength="50" size="50"/>
                        </div>
                    </div>
                </div>

                <div>
                    <div class="info_item"><span>机构电话:</span>
                        <input type="text" name="telphone" id="telphone" maxlength="80"/>
                    </div>
                    <div class="info_item"><span>详细地址:</span>
                        <input type="text" name="locationAddress" id="locationAddress" maxlength="50" class="text_medium"/>
                    </div>
                </div>

                <div>
                    <div class="info_item"><span>组织机构代码:<b>*</b></span>
                        <input type="text" name="zzjgdm" id="zzjgdm" maxlength="50" class="text_medium"/>
                    </div>
                    <div class="info_item"><span>网点所属区域<b>*</b></span>
                        <tags:select name="siteDistrictId" id="siteDistrictId" tableName="T_SYSTEM_DICT" whereClause="CODE_TYPE_ID = 13 "  />
                    </div>
                </div>

                <div>
                    <div class="info_item"><span>银行类别<b>*</b></span>
                        <tags:select name="orgType" id="orgType" tableName="T_SYSTEM_DICT" whereClause="CODE_TYPE_ID = 21 " />
                    </div>
                    <div class="info_item">
                        <span>上级机构:</span> <input type="hidden" name="parentSiteId" id="parentId" />
                        <input type="text" id="parentCode" name="parentCode" size="50"/>
                    </div>
                </div>

            </div>
            <div class="panel_footer">
                <div>
                    <input type="submit" value="保存" class="btn_blue" id="btn_save"/>
                    <input type="button" value="返回" class="btn_blue" id="btn_back"/>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    function autoCompleted(_id){
        $(_id).autocomplete({
            source:function( request, response){''
                $.post("${ctx}/system/organization/getAllByZzjgdm"
                        ,{"parentCode":request.term}
                        , function( data, status, xhr ) {
                            response(
                                    $.map( data, function( item ) {
                                        return {
                                            label:item.orgName+"("+item.zzjgdm+")",
                                            value:item.orgName+"("+item.zzjgdm+")",
                                            id:item.id
                                        }
                                    }) );
                        });
            },
            select: function(event, ui) {
                $("#parentId").val(ui.item.id);
            },
            search:function( event, ui ) {
            },
            change:function( event, ui ) {
            }
        });
    }

    function loadOrg(){
        var _id = "${id}";
        if(_id!="" && _id!="null"){
            $.ajax({
                url:"${ctx}/system/organization/getOrg?random="+ new Date().getTime(),
                data: {"id": _id},
                success: function (data) {
                    $("#form_org").autoMatch(data);
                    var _mode = "${mode}";
                    if(_mode!="" && _mode!="null"){
                        if(_mode == "v"){
                            $("#form_org").readonly();
                        }
                    }
                }
            })
        }
    }

    function gotoOrgListPage(){
        linkTo("${ctx}/system/organization/list");
    }
    $(function (){
        $("#btn_back").bind("click",gotoOrgListPage);
        autoCompleted("#parentCode");
        
        $("#parentSiteId").change(function(){
        	checkParentOrgValid();
        });
        
        $("#form_org").validate({
        	rules:{
        		orgCode: {
                    required:true,
                    isGhsOrgNo:true

                },
        		orgName: "required",
                siteDistrictId: "required",
                orgType: "required",
                zzjgdm: "required"
            },
            messages:{
                orgCode: {
                    required:"14位银行代码必填",
                    isGhsOrgNo:"请正确14位银行代码编号(首字母必须是大写如：C12341234123)"
                },
            	orgName: "机构名称必填",
                siteDistrictId: "网点所属区域必填",
                orgType: "银行类型必填",
                zzjgdm: "组织机构必填"
            },
            submitHandler: function(form) {
                $(form).ajaxSubmit({
                    success: function(response) {
                    	if(response == 'ERROR:SAME'){
                    		$.prompt("保存失败:不能选择自己作为自己的上级机构，请重新选择上级机构.");
                    	} else if(response == 'ERROR:CHILD'){
                    		$.prompt("保存失败:该上级机构已经是属于本机构的下属机构（直接或间接），请重新选择上级机构.");
                    	} else {
	                        $.prompt("保存成功");
	                        window.setTimeout(function(){
	                        	gotoOrgListPage();
	                        },2000);
                    	}
                    },

                    error: function(response){
                        $.prompt("保存失败:"+response,"错误");
                    }
                });
            }
        });

        loadOrg();        

    });

    
    function checkParentOrgValid() {
    	var siteId = $("#id").val();
    	var parentSiteId = $("#parentSiteId").val();
        if(siteId != '' && parentSiteId != '') {
            $.ajax({
                url: "${ctx}/system/organization/parentOrgValidCheck/" + siteId +"/"+ parentSiteId,
                data: {"random": new Date().getTime()},
                success: function (data) {
                    if(data != '') {
                    	$.prompt("该上级机构已经是属于本机构的下属机构（直接或间接），并且不能选择自己，请重新选择上级机构.");
                        return false;
                    } else {
                        return true;
                    }
                }
            });
        }
    }
</script>
